package com.client.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.client.lu.AnalyzerTest;
import com.client.pojo.Post;
import com.client.pojo.PostType;
import com.client.service.HistoryService;
import com.client.service.PostService;

@Controller
public class PostController {
	@Autowired
	private PostService postService;

	 @Autowired
	 private HistoryService historyService;
	
	// 跳转首页
	@RequestMapping("/topostlist")
	public String toLogin(@PathParam("userId")String userId,Model model,HttpSession session) {
		List<Post> postList = postService.findPostAll();
		
		List<PostType> typeList=postService.selectTypeAll();
		
//		System.out.println("********888"+typeList.toString());
		model.addAttribute("postList", postList);
		model.addAttribute("typeList", typeList);
		Integer id=Integer.parseInt(userId);
//		System.out.println(id+"--------------");
		String type=historyService.selectType(id);
//		System.out.println(type);
		List<Post>sendPost=sendPost(type);
		model.addAttribute("sendPost", sendPost);
		
		return "postlist";
	}
	
			

	 public List<Post> sendPost(String type){
		   List<Post> postlist;
		   if(type==null){
			   postlist=historyService.selectfristPost(type);
		   }
		   else{
			   postlist=historyService.selectPost(type);
	  
			   }
		 return postlist;
	 
	 }
	
	
	
	/*
	 * 跳转添加页面
	 */
	@RequestMapping("/toaddPost")
	public String toaddPost(Model model) {
		List<PostType>postList=postService.selectTypeAll();
//		System.out.println(postList);
		model.addAttribute("postList", postList);
		
		return "/form";
	}
	
	/*
	 * 根据id查看详细信息
	 */
	@RequestMapping("/tobloglist")
	public String bloglist(@PathParam("id") String id, Model model,HttpServletRequest request) {
		Integer ID = Integer.parseInt(id);
		Post post = postService.bloglist(ID);
		model.addAttribute("post", post);
//		HttpSession session= request.getSession();
//		String userid=(String) session.getAttribute("userId");
//		Integer userId=Integer.parseInt(userid);
//		Integer typeid=Integer.parseInt(id);
//		System.out.println(userId+typeid+post.getType()+"++++++++++++++++++");
//		historyService.addHistory(null, userId, typeid, post.getType());
		return "bloglist";
	}

	/*
	 * 根据title查询
	 */
	@RequestMapping("/titleSelect")
	public String selectPostByTitle(String title, Model model) {

		List<Post> postList = postService.selectBlogList(title);

		model.addAttribute("postList", postList);
		return "views/design";
	}

	/*
	 * 根据type查询
	 */
	@RequestMapping("/findPostByType")
	public String findPostByType(@PathParam("type") String type, Model model) {
		List<Post> postList = postService.findPostByType(type);
		model.addAttribute("postList", postList);
		return "/design";
	}

	
	@RequestMapping("/checkParam")
	   public String checkParam(Post post,HttpServletResponse response,HttpServletRequest request) throws Exception {
		Date date=new Date();
		
		post.setCreatTime(date);
		post.setGoodNum(0);
//		Integer id = Integer.parseInt(userId);
//		post.setUserId(id);
		  AnalyzerTest analiy=new AnalyzerTest();
			
		 String word=analiy.printAnalyzer(post.getContent());
		   
		   if(word==null){
			   postService.saveList(post); 
			   System.out.println("*****************——————————");
	          return "forward:topostlist";
		   }else{
			   System.out.println(word);
			  response.setContentType("text/html;charset=utf-8");
			  PrintWriter out = response.getWriter();
			  
			 out.print("<script language=\"javascript\">alert('含有敏感词："+word+"，请重新编写');</script>");
			
		   }
	
		   return "forward:toaddPost";
			
		}
	
	@RequestMapping("/toPostList")
	public String toPostList(Model model,HttpServletRequest request){
		
		
		HttpSession  session=  request.getSession();
		String str=session.getAttribute("userId")+"";
	
		Integer userId=Integer.parseInt(str);
//		System.out.println(userId+"*********------");
		List<Post> postList = postService.findPostById(userId);
		model.addAttribute("postList", postList);
//		System.out.println(postList);
		return "design";
		
	}
	
	@RequestMapping("/selectBlogList")
	public String selectBlogList(String keyword,Model model){
		List<Post>postList=postService.selectBlogList(keyword);
		model.addAttribute("postList", postList);
		return "design";
		
	}
	/*
	 * 根据id删除
	 */
	@RequestMapping("/deletePostById")
	public String deletePostById(@PathParam("id")String id){
		Integer ID=Integer.parseInt(id);
		postService.deletePostById(ID);
		return "redirect:toPostList?userId="+id;
		
	}
	/*
	 * 点赞
	 */
	@RequestMapping("/addGoodNum")
	public String addGoodNum(@PathParam("id")String id){
		
		
		Integer ID=Integer.parseInt(id.trim());
		postService.addGoodNum(ID);
		//return "redirect:toPostList?userId="+ID;
		
		return "redirect:/tobloglist?id="+ID;
	}
	
}
